﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GTAFingerprinterCore.Core
{
    public class Config
    {
        /// <summary>
        /// 全屏模式
        /// </summary>
        public bool FullScreenMode { get; set; }

        /// <summary>
        /// 按键延时
        /// </summary>
        public int KeyPressDelay { get; set; }

        /// <summary>
        /// 准确率
        /// </summary>
        public float Similarity { get; set; }

        /// <summary>
        /// 指纹快捷键
        /// </summary>
        [JsonConverter(typeof(StringEnumConverter))]
        public Keys FingerprinterKey { get; set; } = Keys.F10;

        /// <summary>
        /// 连击
        /// </summary>
        [JsonConverter(typeof(StringEnumConverter))]
        public Keys QuickClickKey { get; set; } = Keys.F9;

        public SubsOption Subs { get; set; }

        public BigOption Big { get; set; }

        /// <summary>
        /// 从json文件加载配置文件
        /// </summary>
        /// <param name="filename">配置文件名</param>
        /// <returns>配置实例对象</returns>
        public static async Task<Config> FromJsonFile(string filename)
        {
            try
            {
                var json = await File.ReadAllTextAsync(filename);
                return JsonConvert.DeserializeObject<Config>(json);
            }
            catch (Exception e)
            {
                throw new InvalidOperationException("加载配置文件出错！", e);
            }
        }

        /// <summary>
        /// 保存当前配置实例对象到文件
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <returns></returns>
        public async Task SaveConfig(string filename)
        {
            try
            {
                var json = JsonConvert.SerializeObject(this, Formatting.Indented);
                await File.WriteAllTextAsync(filename, json);
            }
            catch (Exception e)
            {
                throw new InvalidOperationException("保存配置文件出错！", e);
            }
        }
    }
}